package com.res.controller;

import ch.qos.logback.core.net.SyslogOutputStream;
import com.res.entity.Computer;
import com.res.entity.Readroom;
import com.res.service.ComputerService;
import com.res.util.FenyeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("computer")
public class ComputerController {
@Autowired
    private ComputerService computerService;

    @GetMapping
    public Map<String,Object> getAllComputer(Computer computer,Integer page, Integer limit, Integer rid){
        Map<String,Object> map=new HashMap<String, Object>();
        Pageable o= PageRequest.of(page-1,limit);
        Page<Computer> allComputer = computerService.findAllComputer(computer,o, rid);
        long count = computerService.count(computer,o, rid);
        List<Computer> data=allComputer.getContent();
        map.put("data",data);
        map.put("code",0);
        map.put("count",count);
        map.put("msg","");
        return  map;
    }
    @PostMapping
    public Integer addComputer(Computer computer,Integer rid){
        Readroom readroom=new Readroom();
        readroom.setRid(rid);
        computer.setReadRoom(readroom);
        return computerService.addComputer(computer);
    }
    @PutMapping
    public Integer putComputer(Computer computer,Integer rid){
        Readroom readroom=new Readroom();
        readroom.setRid(rid);
        computer.setReadRoom(readroom);
        return computerService.updateComputer(computer);
    }
    @DeleteMapping
    public Integer deleteComputer(Integer cid ){
        return computerService.deleteComputer(cid);
    }


}
